/**********************************************************************
* Filename: CalendarDate.java
* Created: 2012/04/23
* @author Fernando Brito e Abreu
**********************************************************************/
package org.quasar.copaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;

public class CalendarDate
{
	public static Set<CalendarDate> allInstances = new HashSet<CalendarDate>();
	
	private int day;
	private int month;
	private int year;
	
	/**********************************************************************
	* Default constructor
	**********************************************************************/
	public CalendarDate()
	{
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param day the day to initialize
	* @param month the month to initialize
	* @param year the year to initialize
	**********************************************************************/
	public CalendarDate(int day, int month, int year)
	{
		this.day = day;
		this.month = month;
		this.year = year;
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the day of the calendarDate
	**********************************************************************/
	public int day()
	{
		return day;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param day the day to set
	**********************************************************************/
	public void setDay(int day)
	{
		this.day = day;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the month of the calendarDate
	**********************************************************************/
	public int month()
	{
		return month;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param month the month to set
	**********************************************************************/
	public void setMonth(int month)
	{
		this.month = month;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the year of the calendarDate
	**********************************************************************/
	public int year()
	{
		return year;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param year the year to set
	**********************************************************************/
	public void setYear(int year)
	{
		this.year = year;
	}
	
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isAfter(CalendarDate t)
	{
		if (this.year == t.year && this.month == t.month && this.day > t.day)
			return true;
		else if (this.year == t.year && this.month > t.month)
			return true;
		else if(this.year>t.year)
			return true;
		else
			return false;
			
//		return if (self.year = t.year) then if (self.month = t.month) then (self.day > t.day) else (self.month > t.month) endif else (self.year > t.year) endif
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isBefore(CalendarDate t)
	{
		if (this.year == t.year && this.month == t.month && this.day < t.day)
			return true;
		else if (this.year == t.year && this.month < t.month)
			return true;
		else if(this.year<t.year)
			return true;
		else
			return false;
		//	return if (self.year = t.year) then if (self.month = t.month) then (self.day < t.day) else (self.month < t.month) endif else (self.year < t.year) endif
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param x the x to set
	* @param y the y to set
	**********************************************************************/
	public boolean isDivisible(int x, int y)
	{
		if((x/y)*y==x)
			return true;
		//	return (((x div y) * y) = x)
		return false;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isEqual(CalendarDate t)
	{
		if(this.year == t.year && this.month == t.month && this.day == t.day )
			return true;
		return false;
		//	return (((self.year = t.year) and (self.month = t.month)) and (self.day = t.day))
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	**********************************************************************/
	public boolean isLeap()
	{
		if(this.isDivisible(this.year, 400) || this.isDivisible(this.year, 4))
			return true;
		else if(this.isDivisible(this.year, 100))
			return false;
		else if(this.isDivisible(this.year, 4))
			return true;
		//	return if (self.isDivisible(self.year, 400) or self.isDivisible(self.year, 4)) then true else if self.isDivisible(self.year, 100) then false else if self.isDivisible(self.year, 4) then true else false endif endif endif
		return false;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param date the date to set
	**********************************************************************/
	public CalendarDate stringToDate(String date)
	{
		 Integer date_year = Integer.parseInt(date.substring(1,4));
         Integer date_month = Integer.parseInt(date.substring(6,7));
         Integer date_day = Integer.parseInt(date.substring(9,10));
         CalendarDate result=null;
         	
         for(CalendarDate d:CalendarDate.allInstances){
                 if(d.year==date_year&&d.month==date_month&&d.day==date_day){
                         result=d;
                 }
         }
         
         if(result==null){
                 result=new CalendarDate(date_day,date_month,date_year);
         }
         return result;

		//	date_year := date.substring(1,4);
		//	 date_month := date.substring(6,7);
		//	 date_day := date.substring(9,10);
		//	 result := CalendarDate.allInstances->select(cd : CalendarDate | (((cd.year = date_year.toInteger) and (cd.month = date_month.toInteger)) and (cd.day = date_day.toInteger)))->asSequence->first;
		//	 if result.isUndefined then result := new CalendarDate(((('D' + date_year) + date_month) + date_day));
		//	 result.initS(date) end;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public int yearsSince(CalendarDate t)
	{
		int result;
		if(this.month < t.month || this.month == t.month && this.day<t.day)
			result = (this.year-t.year)-1;
		else
			result = this.year-t.year;
		//	return if ((self.month < t.month) or ((self.month = t.month) and (self.day < t.day))) then ((self.year - t.year) - 1) else (self.year - t.year) endif
		return result;
	}
	
	/* (non-Javadoc)
	* @see java.lang.Object#toString()
	*/
	@Override
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString()
	{
		return "CalendarDate [day=" + day + ", month=" + month + ", year=" + year + "]\n";
	}
	
}
